//Copyright (C) 2012 Chirila Alexandru
//This file is part of Mirror FTP
//
//Mirror FTP is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Mirror FTP is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with Mirror FTP.  If not, see <http://www.gnu.org/licenses/>.


#include "file_struct.h"

file_struct::file_struct(){
	record.add(type);
	record.add(name);
	record.add(size);
	record.add(rights);
	record.add(owner);
	record.add(date);
	record.add(mirror);
	ref = Gtk::ListStore::create(record);
	set_model(ref);
	append_column("",type);
	append_column("Name",name);
	append_column("Size",size);
	append_column("Rights",rights);
	append_column("Owner",owner);
	append_column("Date",date);
	append_column("Mirror",mirror);
	ref->set_sort_column(type,Gtk::SORT_ASCENDING);
}
file_struct::~file_struct(){}

void file_struct::new_row(Glib::ustring t,Glib::ustring n,long long s,Glib::ustring r,Glib::ustring o,Glib::ustring d,Glib::ustring m){
	///Add new row to the file structure
	///\param t type
	///\param n name
	///\param s size
	///\param r rights
	///\param o owner
	///\param d last modification date
	///\param m mirror

	Gtk::TreeModel::iterator iter = ref->append();
	Gtk::TreeModel::Row row = *iter;
	row[type]=t;
	row[name]=n;
	row[size]=s;
	row[rights]=r;
	row[owner]=o;
	row[date]=d;
	row[mirror]=m;
}

void file_struct::clear(){
	///Clear the file structure view.

	Gtk::TreeModel::Children  children = ref->children();
	Gtk::TreeModel::Children::iterator iter = children.begin();
	while (iter!=children.end()){
		iter=ref->erase(iter);
	}
}
void file_struct::fill_list(Glib::ustring tmp_file,Glib::ustring parrent){
	///Fill list from temporary file.
	///\param tmp_file address of temporary file
	///\param parrent parrent directory name

	fstream input;
	Glib::ustring t,n,r,o,d,m,tmp,aux;
	int poz_i,poz_f;
	int s,count;
	char temp[256];

	new_row("~",parrent,0,"","","","");

	input.open(tmp_file.c_str(),fstream::in);
	while(1){

		input.getline(temp,256);
		tmp=temp;
		if (tmp.length()<=1)
			break;

		count=0;
		for (unsigned int i=0;i<strlen(temp);i++)
			if (temp[i]==' ')
				count++;
		if (count < 8 )
			continue;

		aux=tmp.substr(0,1);
		if(aux=="d")
			t="d";
		else
			t="f";
		r=tmp.substr(1,9);

		poz_i=tmp.find_first_not_of(" ",10);
		poz_f=tmp.find_first_of(" ",poz_i);

		o=tmp.substr(poz_i,poz_f-poz_i)+" ";

		poz_i=tmp.find_first_not_of(" ",poz_f);
		poz_f=tmp.find_first_of(" ",poz_i);

		o+=tmp.substr(poz_i,poz_f-poz_i)+" ";

		poz_i=tmp.find_first_not_of(" ",poz_f);
		poz_f=tmp.find_first_of(" ",poz_i);

		o+=tmp.substr(poz_i,poz_f-poz_i)+" ";

		poz_i=tmp.find_first_not_of(" ",poz_f);
		poz_f=tmp.find_first_of(" ",poz_i);

		if (t!="d")
			s=atol(tmp.substr(poz_i,poz_f-poz_i).c_str());
		else
			s=0;

		poz_i=tmp.find_first_not_of(" ",poz_f);
		poz_f=tmp.find_first_of(" ",poz_i);

		d=tmp.substr(poz_i,poz_f-poz_i)+"/";

		poz_i=tmp.find_first_not_of(" ",poz_f);
		poz_f=tmp.find_first_of(" ",poz_i);

		d+=tmp.substr(poz_i,poz_f-poz_i)+"/";

		poz_i=tmp.find_first_not_of(" ",poz_f);
		poz_f=tmp.find_first_of(" ",poz_i);

		d+=tmp.substr(poz_i,poz_f-poz_i);

		poz_i=tmp.find_first_not_of(" ",poz_f);
		poz_f=0;
		poz_f=tmp.find_first_of(" ",poz_i);

		n=tmp.substr(poz_i,poz_f-poz_i);

		if (poz_f!=-1){
			t="m";
			poz_i=tmp.find_first_not_of(" ",poz_f);
			poz_f=tmp.find_first_of(" ",poz_i);
			poz_i=tmp.find_first_not_of(" ",poz_f);
			poz_f=tmp.find_first_of(" ",poz_i);
			m=tmp.substr(poz_i,poz_f-poz_i);
		}
		new_row(t,n,s,r,o,d,m);

	}
	input.close();
}


